package code.structure.module.scene.view
{
	import code.main.App;
	import code.main.AppCommonView;
	import code.structure.module.scene.SceneController;
	import code.untils.SceneConfig;
	
	import com.greensock.TweenLite;
	import com.patience.map.isometric.utils.IsoUtils;
	import com.patience.mvc.context.ContextIntance;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	/**
	 *  场景类
	 * @author Yuri
	 * 
	 */
	public class Scene
	{
		private var _isoWorld:YIsoWorld;
		
		private var _controller:SceneController;
		
		public function Scene()
		{
			init();
//			initWorld();
		}
		
		private function init():void{
			
		}
		
		private function initListeners():void{
			App.appstage.addEventListener(Event.RESIZE,onResizeHandler);
//			App.appstage.addEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheelHandler);
		}
		
		private function initWorld():void{
			_isoWorld = new YIsoWorld();
			
			(ContextIntance.getContext().commonView as AppCommonView).appContainer.sceneLayer.addChild(isoWorld);
			isoWorld.init();
			isoWorld.infoManager.refreshBackGround();
			isoWorld.contentX = 0;
			isoWorld.contentY = 0;
			isoWorld.reSetContentRect();
			isoWorld.controller = controller;
			
			(ContextIntance.getContext().commonView as AppCommonView).isoWorld = _isoWorld;
		}
		
		/**
		 *刷新场景数据 
		 * @param data
		 * 
		 */
		public function refreshSceneData(data:Dictionary):void{
//			_d = data;
			if(!isoWorld){
				initWorld();
			}else{
				TweenLite.killTweensOf(isoWorld,true);
				isoWorld.clear();
				isoWorld.isCanDrag = true;
				onResizeHandler();
			}
			initListeners();
			isoWorld.initIsoMouseEvents();
			isoWorld.refreshWorldSceneData(data);
		}
		
		
		/**
		 *鼠标滚轮事件 
		 * @param event
		 * 
		 */
		/*protected function onMouseWheelHandler(event:MouseEvent):void
		{
			if(isoWorld){
				if(event.delta>0 && isoWorld.scale<1){
					isoWorld.scale += .25;
					isoWorld.contentX = 0;
					isoWorld.contentY = 0;
					isoWorld.reSetContentRect();
				}else if(event.delta<0 && isoWorld.scale>.75){
					isoWorld.scale -= .25;
					isoWorld.contentX = 0;
					isoWorld.contentY = 0;
					isoWorld.reSetContentRect();
				}
			}
		}*/
		
		/**
		 *改变场景大小 
		 * @param type -1 减小 1 增大
		 * @return 
		 * 
		 */
		public function changeSceneSizeScale(type:int=1):void{
			TweenLite.killTweensOf(isoWorld,true);
			if(isoWorld){
				if(type>0 && isoWorld.scale<1){
					recordCenterPositionX = (isoWorld.width/2 - isoWorld.contentX)/isoWorld.scale ;
					recordCenterPositionY = (isoWorld.height/2 - isoWorld.contentY)/isoWorld.scale ;
					isoWorld.isCanDrag = false;
					TweenLite.to(isoWorld,SceneConfig.SIZE_CHANGE_DURATION,{scale:1,onUpdate:onSceneSizeTweenUpdateHandler,onComplete:onSceneSizeTweenCompleteHandler});
				}else if(type<0 && isoWorld.scale>SceneConfig.WORLD_MIN_SCALE){
					recordCenterPositionX = (isoWorld.width/2 - isoWorld.contentX)/isoWorld.scale ;
					recordCenterPositionY = (isoWorld.height/2 - isoWorld.contentY)/isoWorld.scale ;
					isoWorld.isCanDrag = false;
					TweenLite.to(isoWorld,SceneConfig.SIZE_CHANGE_DURATION,{scale:SceneConfig.WORLD_MIN_SCALE,onUpdate:onSceneSizeTweenUpdateHandler,onComplete:onSceneSizeTweenCompleteHandler});
				}
			}
		}
		private var recordCenterPositionX:Number;
		private var recordCenterPositionY:Number;
		
		/**
		 * SceneSizetween过程 
		 * 
		 */
		private function onSceneSizeTweenUpdateHandler():void{
			isoWorld.setContentRect(SceneConfig.TOP_PADDING,SceneConfig.BOTTOM_PADDING,SceneConfig.LEFT_PADDING,SceneConfig.RIGHT_PADDING);
			isoWorld.moveContentXYtoCenterByIsoPosition(recordCenterPositionX,recordCenterPositionY);
		}
		
		/**
		 * SceneSizetween结束 
		 * 
		 */
		private function onSceneSizeTweenCompleteHandler():void{
			isoWorld.isCanDrag = true;
		}
		
		/**
		 *移动场景居中 
		 * @param gX
		 * @param gZ
		 * @param isTween
		 * 
		 */
		public function moveWorldCenterBySceneGridXAndGridZ(gX:int,gZ:int,isTween:Boolean = false):void{
			if(!isoWorld) return;
			if(!isTween){
				isoWorld.moveContentXYtoCenterByGridPoint(new Point(gX,gZ));
			}else{
				var cp:Point = IsoUtils.isoToScreen(isoWorld.getIsoWorldPoint3DByGridPosition(new Point(gX,gZ)));
				var tempContentX:Number = isoWorld.width/2 - cp.x*isoWorld.scale;
				var tempContentY:Number = isoWorld.height/2 -cp.y*isoWorld.scale;
				isoWorld.isCanDrag = false;
				TweenLite.to(isoWorld,SceneConfig.CAMAL_MOVE_DURATION,{contentX:tempContentX,contentY:tempContentY,onComplete:onCamalMoveTweenCompleteHandler});
			}
		}
		
		private function onCamalMoveTweenCompleteHandler():void{
			isoWorld.isCanDrag = true;
		}
		/**
		 *舞台大小发生变化 
		 * @param event
		 * 
		 */
		protected function onResizeHandler(event:Event=null):void
		{
			// TODO Auto-generated method stub
			if(isoWorld){
				if(!_controller.sceneModel.isMyScene && _controller.sceneModel.isGMScene){
					isoWorld.reSetContentRect(1);
				}else{
					isoWorld.reSetContentRect();
				}
			}
		}

		/**
		 *添加控制器 方便直接在这里进行派发事件 
		 */
		public function get controller():SceneController
		{
			return _controller;
		}

		/**
		 * @private
		 */
		public function set controller(value:SceneController):void
		{
			_controller = value;
			if(isoWorld) isoWorld.controller = controller;
		}

		/**
		 *世界 
		 */
		public function get isoWorld():YIsoWorld
		{
			return _isoWorld;
		}


	}
}